<?php
/**
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 10:52
 */

namespace app\api\business;


use app\api\controller\UserCommon;
use app\api\model\User;
use think\Exception;
use think\facade\Db;
use think\facade\Request;

class UserBus extends UserCommon
{

    public $model;
    public function __construct(){
        parent::__construct();
        $this->model = new User();
    }


    public function getInfo(){
        return $this->model->field('user_id,username,mobile,photo,sex,realname,birthday,integral,money,unionid')->where('user_id',$this->user_id)->find();
    }

    public function save($where,$data){
        return $this->model->where($where)->save($data);
    }

    public function savePwd($where,$post){
        $pwd = $this->model->where($where)->value('password');
        if (md5($post['old_password']) != $pwd){
            throw new Exception('原密码错误');
        }
        return $this->model->where($where)->save(['password' => md5($post['new_password'])]);
    }

    /**
     * 添加观看直播记录
     * @param $param
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addBrowseLog($param){
        $count = Db::name('browse_log')->where('user_id', $this->user_id)->count();
        if ($count >= 20) {
            Db::name('browse_log')->where('user_id', $this->user_id)->order('id', 'desc')->limit(1)->delete();
        }
        $live = Db::name('browse_log')->where('user_id', $this->user_id)->where('live_id', $param['live_id'])->find();
        if ($live){
            Db::name('browse_log')->where('id', $live['id'])->delete();
        }
        $param['user_id'] = $this->user_id;
        $param['create_time'] = toDate(time());
        Db::name('browse_log')->insert($param);
    }

    /**
     * 获取观看记录
     * @param $type
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBrowseLog($type){
        $where[] = ['user_id', '=', $this->user_id];
        if ($type == 1){
            // 一周
            $start_time = date('Y-m-d', strtotime(date('Y-m-d',time()).' -1 week'));
        } else if ($type == 2){
            // 一月
            $start_time = date('Y-m-d', strtotime(date('Y-m-d',time()).' -1 month'));
        } else if ($type == 3){
            // 三月
            $start_time = date('Y-m-d', strtotime(date('Y-m-d',time()).' -3 month'));
        } else if ($type == 4){
            // 六月
            $start_time = date('Y-m-d', strtotime(date('Y-m-d',time()).' -6 month'));
        } else {
            // 所有
            $start_time = date('Y-m-d', strtotime(date('Y-m-d',time()).' -1 year'));
        }
        $where[] = ['create_time', '>=', $start_time];
        $list = Db::name('browse_log')->where('user_id', $this->user_id)->where('create_time','>=',$start_time)->order('id', 'desc')->select();
        return $list;
    }


    /**
     * 交易记录
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function moneyLog()
    {
        $list = Db::name('money_log')->where('user_id', $this->user_id)->where('status', 1)->order('id', 'desc')->select()->toArray();
        foreach ($list as $k => $v){
            if ($v['type'] == 2 || $v['type'] == 5) {
                $list[$k]['negative'] = 0;
            }else{
                $list[$k]['negative'] = 1;
            }
        }
        return $list;
    }

}